class GuitarIn 
   
  # Attributes
  attr_reader :mfc_class_name
  attr_reader :mfc_class_schema

  attr_reader :system
  attr_reader :staff
  attr_reader :position
  
  # Constants   
  DEFAULT_SYSTEM = 0
  DEFAULT_STAFF = 0
  DEFAULT_POSITION = 0
  DEFAULT_DATA = 0
  
  MIN_SYSTEM = 0
  MAX_SYSTEM = 65535
  
  MIN_STAFF = 0
  MAX_STAFF = 2
  
  MIN_POSITION = 0
  MAX_POSITION = 255

  # Constructor  
  def initialize(system = DEFAULT_SYSTEM, staff = DEFAULT_STAFF, position = DEFAULT_POSITION, staff_guitars = nil, rhythslash_guitars = nil) 

    mfc_class_name = "CGuitarIn"
    mfc_class_schema = 1

    return false unless valid_system?(system)
    return false unless valid_staff?(staff)
    return false unless valid_position?(position)
     
    @data = DEFAULT_DATA

    system=(system)
    staff=(staff)
    position=(position)
    staff_guitars=(staff_guitars)
    rhythslash_guitars=(rhythslash_guitars)
  end
     
  # Methods
  def valid_system?(system)
    return ((system >= MIN_SYSTEM) && (xsystem <= MAX_SYSTEM))
  end
   
  def system=(system) 
    return false unless valid_system?(system)
    @system = system
    return true
  end
   
  def valid_staff?(staff)
    return ((staff >= MIN_STAFF) && (staff <= MAX_STAFF))
  end
   
  def staff=(staff) 
    return false unless valid_staff?(staff)
    @staff = staff
    return true
  end
   
  def valid_position?(position) 
    return ((position >= MIN_POSITION) && (position <= MAX_POSITION))
  end
   
  def position=(position) 
    return false unless valid_position?(position)
    @position = position
    return true
  end
   
  def staff_guitars=(guitars) 
    return (guitars=(false, guitars))
  end
   
  def staff_guitars()
    return guitars(false)
  end
   
  def staff_guitars?() 
    return guitars_set?(false)
  end
   
  def rthythslash_guitars=(guitars) 
    return (guitars=(true, guitars))
  end
   
  def rhythslash_guitars() 
    return guitars(true)
  end
   
  def rhythslash_guitars?() 
    return guitars?(true)
  end
      
  def guitars=(rhythslashes, guitars) 
    if (rhythslashes)
      @data = make_word(guitars, staff_guitars())
    else
      @data = make_word(rhythslash_guitars(), guitars)
    end 
    return true
  end
   
  def guitars(rhythslashes) 
     return (hi_byte(@data)) unless rhythslashes
     return (lo_byte(@data)
  end
   
  def guitars?(rhythslashes) 
    guitars = (rhythslashes) ? rhythslash_guitars() : staff_guitars()
    return (guitars != false)
  end
   
  def text(rhythslashes)
    return_value = ""
    guitars = guitars(rhythslashes)
     
    if (guitars == 0)
      return return_value
    end   
    
    roman_numerals = ""     
    mask = 1
    guitar_count = 0
    guitar_number = 1
    while mask < 64 
      if ((guitars & mask) == mask)         
        if (roman_numerals.length > 0)
          roman_numerals .= ", "
        end
        roman_numerals .= arabic_to_roman(guitar_number, true)
        guitar_count++
      end
      
      mask *= 2
      guitar_number++
    end
     
    if (guitar_count == 1)
      return_value = sprintf("Gtr %s", roman_numerals)
    else
      return_value = sprintf("Gtrs %s", roman_numerals)
    end

    return (return_value)
  end
   
  def arabic_to_roman(number, upper_case) 
    return_value = ""

    if (!((number > 0) && (number < 6000))) 
      return return_value
    end
     
    while (number >= 1000) 
      number -= 1000
      return_value .= "m"
    end
    while (number >= 900) 
      number -= 900
      return_value .= "cm"
    end
    while (number >= 500) 
      number -= 500
      return_value .= "d"
    end
    while (number >= 400) 
      number -= 400
      return_value .= "cd"
    end
    while (number >= 100) 
      number -= 100
      return_value .= "c"
    end
    while (number >= 90) 
      number -= 90
      return_value .= "xc"
    end
    while (number >= 50) 
      number -= 50
      return_value .= "l"
    end
    while (number >= 40) 
      number -= 40
      return_value .= "xl"
    end
    while (number >= 10) 
      number -= 10
      return_value .= "x"
    end
    while (number >= 9) 
      number -= 9
      return_value .= "ix"
    end
    while (number >= 5) 
      number -= 5
      return_value .= "v"
    end
    while (number >= 4) 
      number -= 4
      return_value .= "iv"
    end
    while (number >= 1) 
      number -= 1
      return_value .= "i"
    end
     
    if (upper_case)
      return_value = return_value.uppercase
    end

    return (return_value)
  end

  # Serialization   
  def do_serialize(stream)   
    write_word(stream, @system)
    write_byte(stream, @staff)
    write_byte(stream, @position)
    write_word(stream, @data)
    return true
  end
   
  def do_deserialize(stream)
    @system = read_word(stream)
    @staff = read_byte(stream)
    @position = read_byte(stream)
    @data = read_word(stream)  
  end
    
end
